package com.yn.core.service.impl;

import java.io.Serializable;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.yn.core.dao.BaseDao;
import com.yn.core.service.BaseService;
import com.yn.xr.common.page.Page;

/**
 * 
 * @description:业务处理基类接口实现
 * @fileName:BaseServiceImpl.java
 * @createTime:2015年1月26日 上午11:23:50
 * @author:肖震
 * @version 1.0.0
 * 
 */
public class BaseServiceImpl<T, ID extends Serializable> implements
		BaseService<T, ID> {
	private static final long serialVersionUID = -8262204946332259624L;
	private BaseDao<T, ID> baseDao;

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void delete(Serializable... entityids) {
		baseDao.delete(entityids);

	}

	@Transactional(propagation = Propagation.REQUIRED)
	public Serializable saveBackId(T entity) {
		return baseDao.saveBackId(entity);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void delete(T entity) {
		baseDao.delete(entity);
	}

	@Override
	// @Transactional(propagation = Propagation.REQUIRED)
	public T findById(Serializable entityId) {
		// TODO Auto-generated method stub
		return (T) baseDao.findById(entityId);
	}

	@Override
	public List<T> find(String hql) {
		// TODO Auto-generated method stub
		return baseDao.find(hql);
	}

	@Override
	public List<T> findByEntity(T entiey) {
		// TODO Auto-generated method stub
		return baseDao.findByEntity(entiey);
	}

	@Override
	public List<T> findByProperty(String propertyName, Object value) {
		// TODO Auto-generated method stub
		return baseDao.findByProperty(propertyName, value);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return baseDao.getCount();
	}

	@Override
	public Page<T> page(int firstindex, int maxresult, String wherejpql,
			Object[] queryParams, LinkedHashMap<String, String> orderby) {
		Page<T> page = new Page<T>(maxresult, firstindex);
		page.setQueryResult(baseDao.getScrollData(page.getFirstResult(),
				maxresult, wherejpql, queryParams, orderby));
		return page;

	}

	@Override
	public Page<T> page(int firstindex, int maxresult, String wherejpql,
			LinkedHashMap<String, String> orderby) {
		Page<T> page = new Page<T>(maxresult, firstindex);
		page.setQueryResult(baseDao.getScrollData(page.getFirstResult(),
				maxresult, wherejpql, orderby));
		return page;

	}

	@Override
	public Page<T> page(int firstindex, int maxresult, String wherejpql,
			Object[] queryParams) {
		Page<T> page = new Page<T>(maxresult, firstindex);
		page.setQueryResult(baseDao.getScrollData(page.getFirstResult(),maxresult, wherejpql, queryParams));
		return page;

	}

	@Override
	public Page<T> page(int firstindex, int maxresult,
			LinkedHashMap<String, String> orderby) {
		Page<T> page = new Page<T>(maxresult, firstindex);
		page.setQueryResult(baseDao.getScrollData(page.getFirstResult(),
				maxresult, orderby));
		return page;
	}

	@Override
	public Page<T> page(int firstindex, int maxresult) {
		Page<T> page = new Page<T>(maxresult, firstindex);
		page.setQueryResult(baseDao.getScrollData(page.getFirstResult(),
				maxresult));
		return page;

	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void deleteAll(Collection<T> entities) {

		baseDao.deleteAll(entities);

	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void save(T entity) {
		baseDao.save(entity);

	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void saveOrUpdateAll(Collection<T> entities) {
		baseDao.saveOrUpdateAll(entities);

	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void update(T entity) {
		baseDao.update(entity);

	}

	@Transactional(propagation = Propagation.REQUIRED)
	public void saveOrUpdate(T entity) {
		baseDao.saveOrUpdate(entity);
	}

	public BaseDao<T, ID> getBaseDao() {
		return baseDao;
	}

	public void setBaseDao(BaseDao<T, ID> baseDao) {
		this.baseDao = baseDao;
	}

	@Override
	public Page<T> page(int firstindex, int maxresult, String hql) {
		Page<T> page = new Page<T>(maxresult, firstindex);
		page.setQueryResult(baseDao.getScrollData(page.getFirstResult(),
				maxresult));
		return page;
	}

	@Override
	public List getSQLScrollData(String sql) {

		return baseDao.getSQLScrollData(sql);
	}

	@Override
	public List getSQLScrollData(final String sql, final Object[] params) {
		return baseDao.getSQLScrollData(sql, params);
	}

	@Override
	public List findForHql(String hql) {
		// TODO Auto-generated method stub
		return baseDao.findForHql(hql);
	}

	@Override
	public List<T> findPre(String hql, Object... paramValues) {

		return baseDao.findPre(hql, paramValues);
	}

	@Override
	public List findForHqlPre(String hql, Object... paramValues) {

		return baseDao.findForHqlPre(hql, paramValues);
	}

	@Override
	public Page<T> page(String preHql, int firstindex, int maxresult,
			String wherejpql, Object[] queryParams,
			LinkedHashMap<String, String> orderby) {
		Page<T> page = new Page<T>(maxresult, firstindex);
		page.setQueryResult(baseDao.getScroll(preHql, page.getFirstResult(),
				maxresult, wherejpql, queryParams, orderby));
		return page;

	}

}
